package com.flow.platfrom.action;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.struts2.ServletActionContext;
import com.flow.model.BackInfo;
import com.flow.model.UserBasic;
import com.flow.service.IUserBasicService;
import com.flow.tools.StringTools;

/**
 * 会员基本信息Action
 * 
 * @author wang
 * @2015-12-6 UserBasicAction.java
 * 
 */
public class UserBasicAction {

	/**
	 * 服务引用
	 */
	private IUserBasicService<UserBasic> userBasicService;
	
	/**
	 * 注册
	 */
	public void insertUser() {

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		PrintWriter out = null;
		BackInfo back = new BackInfo();

		try {
			String codepic = request.getParameter("txtCode");
			if (StringTools.nullOrEmpty(codepic)) {
				String sessionCode = session.getAttribute("validatecode") == null ? ""
						: session.getAttribute("validatecode").toString();
				if (!codepic.equalsIgnoreCase(sessionCode)) {
					back.setBackCode("001");
				}
				Thread.sleep(5000);
				String loginid = request.getParameter("loginid");
				String loginpass = request.getParameter("loginpass");
				UserBasic userBasic = new UserBasic();
				userBasic.setUbName(loginid);
				userBasic.setUbPwd(loginpass);
				if (userBasicService.insertUserBasic(userBasic)) {
					back.setBackCode("000");
				} else {
					back.setBackCode("001");
				}

			}
			out = ServletActionContext.getResponse().getWriter();
			JSON json = JSONSerializer.toJSON(back);
			out.print(json);
			out.flush();
			out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.removeAttribute("validatecode");
		}
		
	}

	/**
	 * 验证用户名是否可用
	 */
	public void validateLoginName() {
//
//		HttpServletRequest request = ServletActionContext.getRequest();
//		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
//		PrintWriter out = null;
//		String name = request.getParameter("loginid");
//		BackInfo back = new BackInfo();
		try {

			//out = ServletActionContext.getResponse().getWriter();
//			if (userBasicService.validateName(name)) {
//				back.setBackCode("000");
//				back.setBackMsg("可以使用.");
//			} else {
//				back.setBackCode("001");
//				back.setBackMsg("用户名已存在.");
//			}
//			JSON json = JSONSerializer.toJSON(back);
//			out.print(json);
//			out.flush();
//			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 验证验证码
	 */
	public void validateCode() {

		System.out.println("vlidateCode");
		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = null;
		try {
			out = ServletActionContext.getResponse().getWriter();
			String inputCode = request.getParameter("txtCode");
			String sessionCode = session.getAttribute("validatecode") == null ? ""
					: session.getAttribute("validatecode").toString();
			BackInfo back = new BackInfo();
			if (StringTools.nullOrEmpty(inputCode)
					&& StringTools.nullOrEmpty(sessionCode)) {
				if (inputCode.equals(sessionCode)) {
					back.setBackCode("000");
					back.setBackMsg("验证码正确.");

				} else {
					back.setBackCode("001");
					back.setBackMsg("验证码错误.");
				}
			} else {
				back.setBackCode("001");
				back.setBackMsg("请输入验证码.");
			}
			JSON json = JSONSerializer.toJSON(back);
			out.print(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setUserBasicService(IUserBasicService<UserBasic> userBasicService) {
		this.userBasicService = userBasicService;
	}
}
